package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.repository.condition.UserTicketRefCondition;
import com.spa.infrastructure.repository.mapper.UserTicketRefMapper;
import com.spa.infrastructure.repository.po.UserTicketRefPO;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class UserTicketRefRepositoryService extends ServiceImpl<UserTicketRefMapper, UserTicketRefPO> {

    @Autowired
    private UserTicketRefMapper userTicketRefMapper;

    public Long addTicket(UserTicketRefPO po) {
        userTicketRefMapper.insert(po);
        return po.getId();
    }

    public Page<UserTicketRefPO> page(UserTicketRefCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    public List<UserTicketRefPO> list(UserTicketRefCondition condition) {
        return list(wrapper(condition));
    }

    public UserTicketRefPO getUserTicketRefById(Long id) {
        return getById(id);
    }

    public List<UserTicketRefPO> getUserTicketRefByUserId(Long userId) {
        LambdaQueryWrapper<UserTicketRefPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(UserTicketRefPO::getUserId, userId);
        wrapper.eq(UserTicketRefPO::getIsDel, WhetherTypeEnum.$0.getCode());
        return list(wrapper);
    }

    private LambdaQueryWrapper<UserTicketRefPO> wrapper(UserTicketRefCondition condition) {
        LambdaQueryWrapper<UserTicketRefPO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(condition.getName()),UserTicketRefPO::getName,condition.getName());
        return wrapper;
    }

    public boolean delUserTicketByUserId(Long userId) {
        LambdaUpdateWrapper<UserTicketRefPO> wrapperUp = Wrappers.lambdaUpdate();
        wrapperUp.eq(UserTicketRefPO::getUserId, userId);
        return remove(wrapperUp);
    }

    public boolean delUserTicketRefByTicketIdAndUserId(Long ticketId, Long userId) {
        LambdaQueryWrapper<UserTicketRefPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserTicketRefPO::getUserId, userId);
        wrapper.eq(UserTicketRefPO::getTicketId, ticketId);

        List<UserTicketRefPO> list = list(wrapper);
        if(CollUtil.isNotEmpty(list)){
            return removeById(list.get(0));
        }
        return false;
    }


    public boolean addBatchUserTicket(List<UserTicketRefPO> poList) {
        return saveBatch(poList);
    }

    public boolean updateIsDelById(Integer delFlag ,Long id) {
        LambdaUpdateWrapper<UserTicketRefPO> wrapperUp = Wrappers.lambdaUpdate();
        wrapperUp.set(UserTicketRefPO::getIsDel,delFlag);
        wrapperUp.eq(UserTicketRefPO::getId,id);
        return update(wrapperUp);
    }

}
